<?php

namespace App\Models;

use App\Http\Resources\RsMsgManageResource;
use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;

class RsMsgManage extends Model
{
    use HasFactory;
    public $timestamps=false;
    protected $table='rs_msg_manage';


    //查询消息数据列表
    public static function lists($page = 1, $limit = 20, $params = []){
        $ary_where = array();
        $news_type=data_get($params,'news_type',false);

        if($news_type){
            $ary_where[] = ['msg_type', 'like', '%'.$news_type.'%'];
        }
        $query = self::where($ary_where);
        $count = $query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (RsMsgManage $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();

            $rebate['status'] = $rebate['status']==0 ?'否' : '是';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;

    }

    //对消息进行添加操作
    public static function createApi($data){

        if(! $id=data_get($data,'id',false)){
            $model=new self();
            $model->msg_group=$data['msg_group'];
            $model->msg_type=$data['msg_type'];
            $model->status=$data['status'];
            $model->api_url=$data['api_url'];
            $model->create_time=date('Y-m-d');
        }else{
            $model = self::where(['id'=>$id])->first();
            $model->msg_group=$data['msg_group'];
            $model->msg_type=$data['msg_type'];
            $model->status=$data['status'];
            $model->api_url=$data['api_url'];
           // $model->create_time=date('Y-m-d');
        }

       return $model->save() ?true:false;
    }

    //查询自定义消息下的分组信息
    public static function getType($user_id){
        $ary_where = array();
        $ary_where[]=['status','=',1];
        $query=self::where($ary_where);

        $data = $query->orderBy('id', 'asc')->groupBy('msg_group')->get()->map(function (RsMsgManage $rebate) use($user_id){
            $rebate = $rebate->attributesToArray();
            //查询出当前分组下的的所有id
            $count_list=RsMsgManage::query()->where(['msg_group'=>$rebate['msg_group']])->get();

            foreach ($count_list as $k=>$v){
               $a[]=$v['id'];
            }
            $rebate['msgcount']=RsMsginfo::query()->where('recevier_id','=',$user_id)->whereIn('msg_type_id',$a)->count();
            $rebate['status'] = $rebate['status']==0 ?'否' : '是';

            return $rebate;
        })->toArray();
        return $data ? compact( 'data') : false;

    }

    //查询相关数据
    public static function getMsg($page,$pagesize=0,$params = []){
        $user_id=data_get($params,'user_id',false);
        $content=data_get($params,'content',false);
        $daytime=data_get($params,'daytime',false);
        $type_id=data_get($params,'type_id');

            //查询当前下的二级数据及以下数据
            //获取每个类别


            $msglist=RsMsgManage::with('getMsgInfo')
                ->where('id','=',$type_id)
                ->where('status','=',1)
                  ->with('getMsgInfo',function ($query) use($user_id,$page,$pagesize,$content,$daytime){
                      $ary_where = array();
                      $ary_where[]=['recevier_id','=',$user_id];
                      if($content){
                          $ary_where[]=['msg_title','like',"%{$content}%"];
                      }
                      if($daytime){
                          $ary_where[]=['msg_title','like',"%{$daytime}%"];
                      }
                      $query->where($ary_where);

                      $query->offset(CommonFun::getOffset($page, $pagesize))->limit($pagesize)->orderBy('id', 'desc');
                     // return compact('data','count');
                  })->withCount(['getMsgInfo' => function ($query)use($user_id,$content,$daytime) {
                    $ary_where = array();
                    $ary_where[]=['recevier_id','=',$user_id];
                    if($content){
                        $ary_where[]=['msg_title','like',"%{$content}%"];
                    }
                    if($daytime){
                        $ary_where[]=['msg_title','like',"%{$daytime}%"];
                    }
                    $query->where($ary_where);

                }])->get();



            //将数据进行优化处理
           // var_dump($msglist);
            $data=RsMsgManageResource::collection($msglist);

        return $data;
    }


    //获取发送的信息
    public function getMsgInfo(){
        return $this->hasMany(RsMsginfo::class,'msg_type_id','id');
    }

    //查询消息详情
//    public static function infos($id){
//        $msglist=RsMsgManage::with('getMsgInfo')
//            ->where('id','=',$id)
//            ->where('status','=',1)
//            ->get();
//        $data=RsMsgManageResource::collection($msglist);
//        return $data;
//    }

}
